<template>
  <div class="loading-spinner" :class="{ 'loading-spinner--overlay': overlay }">
    <div class="loading-spinner__content">
      <div
        class="loading-spinner__icon"
        :style="{ width: size + 'px', height: size + 'px' }"
      >
        <svg viewBox="0 0 50 50" class="loading-spinner__svg">
          <circle
            cx="25"
            cy="25"
            r="20"
            fill="none"
            :stroke="color"
            stroke-width="4"
            stroke-linecap="round"
            stroke-dasharray="31.416"
            stroke-dashoffset="31.416"
            class="loading-spinner__circle"
          />
        </svg>
      </div>
      <div v-if="text" class="loading-spinner__text" :style="{ color }">
        {{ text }}
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
interface Props {
  size?: number;
  color?: string;
  text?: string;
  overlay?: boolean;
}

withDefaults(defineProps<Props>(), {
  size: 40,
  color: '#1989fa',
  text: '',
  overlay: false,
});
</script>

<style scoped>
.loading-spinner {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100px;
}

.loading-spinner--overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.8);
  z-index: 9999;
  min-height: 100vh;
}

.loading-spinner__content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.loading-spinner__icon {
  display: flex;
  align-items: center;
  justify-content: center;
}

.loading-spinner__svg {
  width: 100%;
  height: 100%;
  animation: loading-spinner-rotate 2s linear infinite;
}

.loading-spinner__circle {
  animation: loading-spinner-dash 1.5s ease-in-out infinite;
}

.loading-spinner__text {
  font-size: 14px;
  font-weight: 500;
}

@keyframes loading-spinner-rotate {
  100% {
    transform: rotate(360deg);
  }
}

@keyframes loading-spinner-dash {
  0% {
    stroke-dasharray: 1, 150;
    stroke-dashoffset: 0;
  }
  50% {
    stroke-dasharray: 90, 150;
    stroke-dashoffset: -35;
  }
  100% {
    stroke-dasharray: 90, 150;
    stroke-dashoffset: -124;
  }
}
</style>
